﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Data;

namespace BUS
{
    public class XuLyTaiKhoan
    {
        // Tim cac nhan vien chua tao tai khoan
        public static DataTable TimDSNVChuaTaoTK()
        {
            DataTable dt = new DataTable();

            DAO.CDataProvider reader = new DAO.CDataProvider();
            String sql = "SELECT * FROM NHANVIEN WHERE MaNhanVien NOT IN ( SELECT MaNhanVien FROM TAIKHOAN ) ";

            try
            {
                dt = reader.DocDuLieu(sql);
            }
            catch (System.Exception )
            {
            	
            }

            return dt;
        }
        // Them tai khoan
        public static Boolean ThemTaiKhoan(DTO.CTAIKHOAN tk)
        {
            DAO.CDataProvider writer = new DAO.CDataProvider();
            String insertSQL = "INSERT INTO TAIKHOAN(TenDangNhap,MatKhau,MaNhanVien) "
                          + "VALUES('" + tk.Username.ToString() + "',"
                          + "'" + tk.Password.ToString() + "',"
                          + tk.MaNhanVien.ToString() + ")";

            if (writer.ThucThiDuLieu(insertSQL))
            {
                return true;
            }
            else
                return false;
        }

        // Xoa tai khoan
        public static Boolean XoaTaiKhoan(int MaNV)
        {
            DAO.CDataProvider writer = new DAO.CDataProvider();

            // Cau lenh SQL
            String deleteSQL = " DELETE FROM TAIKHOAN "
                + " WHERE MaNhanVien =" + MaNV.ToString() + " ";

            // Thuc thi cau lenh insert
            if (writer.ThucThiDuLieu(deleteSQL))
                return true;
            else
                return false;
        }

        // Sua tai khoan
        public static Boolean SuaTaiKhoan(DTO.CTAIKHOAN tk,int MaNV)
        {
            DAO.CDataProvider writer = new DAO.CDataProvider();

            // Cau lenh SQL
            String updateSQL = "UPDATE TAIKHOAN SET MatKhau='" + tk.Password.ToString() + "' WHERE TenDangNhap='" + tk.Username.ToString() + "' AND MaNhanVien=" + MaNV.ToString();

            // Thuc thi cau lenh insert
            if (writer.ThucThiDuLieu(updateSQL))
                return true;
            else
                return false;
        }

        // tim tai khoan theo ten dang nhap
        public static DTO.CTAIKHOAN TimTaiKhoanTheoUsername(String User, String Pass)
        {
            DTO.CTAIKHOAN u = new DTO.CTAIKHOAN();
            DAO.CDataProvider reader = new DAO.CDataProvider();

            String sql = " SELECT * FROM TAIKHOAN " +
            " WHERE TenDangNhap = '" + User.ToString() + "' AND MatKhau = '" + Pass.ToString() + "' ";

            try
            {
                DataTable dt = reader.DocDuLieu(sql);
                if (dt.Rows.Count == 1)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        u.Username = dr["TenDangNhap"].ToString();
                        u.Password = dr["MatKhau"].ToString();
                        u.MaNhanVien = int.Parse(dr["MaNhanVien"].ToString());
                    }
                }
                else
                {
                    u.Username = "";
                    u.Password = "";
                    u.MaNhanVien = 0;
                }
            }
            catch (System.Exception )
            {
            }

            return u;
        }

        // Hash an input string and return the hash as
        // a 32 character hexadecimal string.
        public static string getMd5Hash(string input)
        {
            // Create a new instance of the MD5CryptoServiceProvider object.
            MD5 md5Hasher = MD5.Create();

            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();
        }

        // Verify a hash against a string.
        public static bool verifyMd5Hash(string input, string hash)
        {
            // Hash the input.
            string hashOfInput = getMd5Hash(input);

            // Create a StringComparer an compare the hashes.
            StringComparer comparer = StringComparer.OrdinalIgnoreCase;

            if (0 == comparer.Compare(hashOfInput, hash))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
